package sort;

import java.util.Arrays;

public class QuickSort {
    public static void main(String[] args) {
        int arr[] = {29, -9, 83, 123, 22, 10, 10, 37, 29};
        sort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }

    private static void sort(int[] arr, int left, int right) {
      //  if (left < 0 || right >= arr.length) return;
        int mid = (left+right)/2;
        int l = left;
        int r = right;
        while (l < r) {
            // 左边找 大于 key 的
            while (l < mid && arr[l] <= arr[mid]) {
                l++;
            }
            while (r > mid && arr[r] >= arr[mid]) {
                r--;
            }

            int temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
        }
        if (left < mid - 1)
            sort(arr, left, mid - 1);
        if (right > mid + 1)
            sort(arr, mid + 1, right);
    }
}
